﻿/*+===================================================================
File:      BossOneHeart.cpp

Summary:   Định nghĩa các phương thức của CBossOneHeart.
===================================================================+*/

#include "BossOneHeart.h"
#include "PlayState.h"
#include "Explosion.h"

CBossOneHeart::CBossOneHeart(D3DXVECTOR2 pos, int id, int typeId) : CMoveableObject(pos, id, typeId)
{
	m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BOSS_ONE_HEART);
	m_nLife = BOSS_ONE_HEART_BLOOD;
	m_box.width = 46;
	m_box.height = 60;
	m_pExplosion = new CExplosionGroup(m_pos, id, ID_BIG_EXPLOSION, 0.68f, ID_BIG_EXPLOSION);
}

CBossOneHeart::~CBossOneHeart()
{
	if (m_pExplosion)
		delete m_pExplosion;
}

void CBossOneHeart::UpdateBox(float timeFrame)
{
	m_box.x = m_pos.x - m_pSprite->GetWidth() / 2.0f + 12.0f;
	m_box.y = m_pos.y + m_pSprite->GetHeight() / 4.0f;
	m_box.vX = m_v.x;
	m_box.vY = m_v.y;
}

void CBossOneHeart::Update(float timeFrame)
{
	if (!m_bIsLive)
	{
		if (m_fTimeDie > 0)
		{
			m_fTimeDie -= timeFrame;
			this->Explode(timeFrame);
		}
		else if (m_fTimeDie < 0)
		{
			m_fTimeDie = 0.0f;
		}
		return;
	}
	//Check blood and kill object
	if (m_nLife <= 0)
	{
		this->m_fTimeDie = 0.4f;
		this->Kill();
	}
	UpdateBox(timeFrame);
}

void CBossOneHeart::UpdateCollision(vector<CGameObject*> vecGameObjs, float timeFrame)
{
	if (!m_bIsLive)
		return;

	float timeCollision = timeFrame;
	CGameObject* objCollision = NULL; // Đối tượng va chạm sớm nhất.
	float normalx = 0.0f, normaly = 0.0f;

	// Xử lí va chạm với đạn của Bill
	for (int i = 0; i < CPlayState::s_billBullets.size(); i++)
	{
		if (CPlayState::s_billBullets[i]->IsLive())
		{
			float t = CheckCollision(CPlayState::s_billBullets[i], normalx, normaly, timeFrame);

			if (t < timeFrame && t < timeCollision)
			{
				// Tìm đối tượng và thời gian va chạm sớm nhất.
				timeCollision = t;
				objCollision = CPlayState::s_billBullets[i];
			}
		}
	}

	// Xử lí va chạm
	if (objCollision != NULL)
	{
		m_nLife--;
		objCollision->Kill();
	}
	
}

void CBossOneHeart::Explode(float timeFrame)
{
	if (m_aPosEx.size() < 8)
	{
		m_aPosEx.push_back(D3DXVECTOR2(m_pos.x - 55.0f, m_pos.y + 16.75f));
		m_aPosEx.push_back(D3DXVECTOR2(m_pos.x - 27.5f, m_pos.y + 16.75f));
		m_aPosEx.push_back(D3DXVECTOR2(m_pos.x + 27.5f, m_pos.y + 16.75f));
		m_aPosEx.push_back(D3DXVECTOR2(m_pos.x + 55.0f, m_pos.y + 16.75f));
		m_aPosEx.push_back(D3DXVECTOR2(m_pos.x - 55.0f, m_pos.y - 16.75f));
		m_aPosEx.push_back(D3DXVECTOR2(m_pos.x - 27.5, m_pos.y - 16.75f));
		m_aPosEx.push_back(D3DXVECTOR2(m_pos.x + 27.5f, m_pos.y - 16.75f));
		m_aPosEx.push_back(D3DXVECTOR2(m_pos.x + 55.0f, m_pos.y - 16.75f));
		m_pExplosion->SetArrayPos(m_aPosEx);
	}
	m_pExplosion->Update(timeFrame);
}
